//
//  FourController.swift
//  fho
//
//  Created by 郭颢源 on 2021/3/19.
//

import UIKit

class FourController: UIViewController, UIScrollViewDelegate{
    
    let items = ["我的发货","我的收货"]
    lazy var segmented:UISegmentedControl = {
        var _segmented = UISegmentedControl(items:items)
        _segmented.selectedSegmentIndex = 0 //默认选中第二项
        //添加值改变监听
        _segmented.addTarget(self, action: #selector(segmentDidchange(_:)),for: .valueChanged)
        return _segmented
    }()
    
    //选项除了文字还可以是图片
    @objc func segmentDidchange(_ segmented:UISegmentedControl){
        //获得选项的索引
        print(segmented.selectedSegmentIndex)
        //获得选择的文字
        let title = segmented.titleForSegment(at: segmented.selectedSegmentIndex)!
        print(title)
        
        self.scrollView.setContentOffset(CGPoint(x: CGFloat(segmented.selectedSegmentIndex)*self.scrollView.frame.width, y: 0), animated: true)
    }
    
    let layout = JSCollectionViewLayout()
    
    let scrollView = UIScrollView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.naviBarStyle()
        self.edgesForExtendedLayout = .init()
        view.backgroundColor = .white

        self.navigationItem.titleView = self.segmented
//        newUserGuide(self.navigationItem.titleView!)

        view.addSubview(scrollView)
//        scrollView.isScrollEnabled = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.delegate = self
        scrollView.isPagingEnabled = true
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        let container = UIView()
        scrollView.addSubview(container)
        container.snp.makeConstraints { (make) in
            make.edges.equalTo(scrollView)
            make.height.equalTo(scrollView)
        }
        
        let view1 = JSMyGoods1View()
        container.addSubview(view1)
        view1.snp.makeConstraints { (make) in
            make.top.left.bottom.equalToSuperview()
            make.width.equalTo(UIScreen.main.bounds.width)
        }
        let view2 = JSMyGoods2View()
        container.addSubview(view2)
        view2.snp.makeConstraints { (make) in
            make.left.equalTo(view1.snp.right)
            make.top.right.bottom.equalToSuperview()
            make.width.equalTo(UIScreen.main.bounds.width)
        }

    

    }
//    func scrollViewDidScroll(_ scrollView: UIScrollView) {
//        //print("scrollView滚动时调用，只要offset的值发生变化就调用")
//        // 函数floor的作用是返回比小的整数
//        let page = floor((scrollView.contentOffset.x)/scrollView.frame.width)
//
//        self.segmented.selectedSegmentIndex = Int(page)
//    }

    //滚动代理 scroll减速完毕调用
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let page = floor((scrollView.contentOffset.x)/scrollView.frame.width)
        
        self.segmented.selectedSegmentIndex = Int(page)
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destination.
     // Pass the selected object to the new view controller.
     }
     */
    
}
